<template>
  <div>
    <el-dialog :visible.sync="$parent.open" title="选择盘点对象" width="70%">
      <div align="center">
        <h3>总盘 {{ total }} 件 / 抽盘比例 {{ formData.proportion * 100 }}% / 需盘 {{ num }}
          件
          / 已选 {{
              formData.stktakingTaskEquipments &&
              formData.stktakingTaskEquipments.length
              || 0
          }}
          件</h3>
      </div>
      <br>
      <el-table v-loading="loading" ref="multipleTable" :data="tableData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="序号" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="资产编号" align="center" prop="noticeType" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.assetNum || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="设备编号" align="center" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.equipmentNum || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="设备名称" align="center" min-width="130">
          <template slot-scope="scope">
            {{ scope.row.equipmentName || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="负责人" align="center" min-width="220">
          <template slot-scope="scope">
            {{ scope.row.headName || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="品牌" align="center" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.brand || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="规格型号" align="center" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.model || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="所属部门" align="center" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.deptName || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="位置" align="center" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.placeName || '-' }}
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
      <div slot="footer" align="center">
        <el-button @click="() => $parent.open = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// import { getsystemlist, getuserlist } from "@/api/equipment/equipment.js";
import { postCreatedTask, getbyDeptIdsList, putUpdateTask, getbyDeptIdsTaskList } from "@/api/Inventory/Inventory.js";
export default {
  components: {},
  props: ['planId', 'dataObj', 'title', 'deptId'],
  data () {
    return {
      formData: {
        deptId: undefined,
        headId: undefined,
        stktakingDeptId: undefined,
        type: undefined,
        proportion: undefined,
        generationType: undefined,
      },
      tableData: [],
      num: 0,
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'equipmentAccountId',
        isAsc: 'desc'//倒叙
      },
      total: 0
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getList();
  },
  mounted () { },
  methods: {
    async getList () {
      this.loading = true

      let params = {
        ...this.queryParams,
        deptId: this.deptId
      }
      let data = await getbyDeptIdsList(params)
      this.tableData = data.data
      this.formData = { ...this.dataObj }
      console.log(this.formData);
      this.total = data.total
      this.num = Math.round(this.total * this.formData.proportion)
      this.loading = false
      if (this.title === '修改任务') {
        let promise = {
          taskId: this.dataObj.taskId
        }
        // this.formData.stktakingTaskEquipments = []
        let data = await getbyDeptIdsTaskList(promise)
        let ArrTable = data && data.data.map((item) => {
          return item.equipmentAccountId
        })
        ArrTable.forEach(element => {
          this.tableData.forEach(item => {
            if (element === item.equipmentAccountId) {
              this.$nextTick(() => {
                this.$refs.multipleTable.toggleRowSelection(item, true);
              })
            }
          });
        });


      }
    },
    handleSelectionChange (val) {
      this.formData.stktakingTaskEquipments = val
      // .map(item => {
      //   return {
      //     equipmentAccountId: item.equipmentAccountId
      //   }
      // })
    },
    handleConfirm () {
      if (this.formData.stktakingTaskEquipments.length === 0) return this.$message.error('请选择盘点对象')
      if (this.formData.stktakingTaskEquipments.length !== this.num) return this.$message.error('请按需盘件数盘点')
      if (this.title === '修改任务') {
        let params = {
          ...this.formData,
          planId: this.planId,
          stktakingTaskEquipments: this.formData.stktakingTaskEquipments.map(item => {
            return {
              equipmentAccountId: item.equipmentAccountId
            }
          })
        }
        putUpdateTask(params).then(() => {
          this.$parent.open = false
          this.$message.success('操作成功')
          this.$parent.$parent.getList()
          this.$parent.$parent.open = false
        })
      } else {
        let params = {
          ...this.formData,
          planId: this.planId,
          stktakingTaskEquipments: this.formData.stktakingTaskEquipments.map(item => {
            return {
              equipmentAccountId: item.equipmentAccountId
            }
          })
        }
        postCreatedTask(params).then(() => {
          this.$parent.open = false
          this.$parent.$parent.getList()
          this.$message.success('操作成功')
          this.$parent.$parent.open = false
        })
      }
    },
  }
}

</script>
<style lang="scss" scoped>
.margin_div {
  margin: 0 auto;
  width: 50%;
}
</style>